package com.weimob.mcloud.wechatwork.customer.convertor.customer;

import com.weimob.mcloud.wechatwork.core.adapter.enums.onecrm.SearchSortFieldEnum;
import com.weimob.mcloud.wechatwork.core.adapter.enums.onecrm.SearchSortTypeEnum;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.onecrm.request.CorpWxUserInfoRequest;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.onecrm.request.CustomerSearchDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.base.BasicInfo;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.ExternalUserInfoFindDTO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.Objects;

public class CustomerRequestBuild {

    /**
     * 构建搜索客搜请求参数
     * @param corpId
     * @param externalUserInfoListDTO
     * @return
     */
    public static CustomerSearchDTO buildOneCrmSearchRequest(String corpId, ExternalUserInfoFindDTO externalUserInfoListDTO) {
        CustomerSearchDTO customerSearchDTO = new CustomerSearchDTO();
        BasicInfo basicInfo = externalUserInfoListDTO.getBasicInfo();
        customerSearchDTO.setBosId(basicInfo.getBosId());
        customerSearchDTO.setProductId(basicInfo.getProductId());
        customerSearchDTO.setPiid(basicInfo.getProductInstanceId());
        customerSearchDTO.setVid(basicInfo.getVid());
        customerSearchDTO.setCorpWxAppId(corpId);
        customerSearchDTO.setAuthorizeVidList(externalUserInfoListDTO.getAuthorizeVidList());
        customerSearchDTO.setExternalUserIds(externalUserInfoListDTO.getExternalUserIds());
        if(StringUtils.isNotBlank(externalUserInfoListDTO.getNickName())){
            customerSearchDTO.setNickName(externalUserInfoListDTO.getNickName());
        }
        customerSearchDTO.setGender(externalUserInfoListDTO.getGender());
        customerSearchDTO.setAttrIdList(externalUserInfoListDTO.getAttIds());
        customerSearchDTO.setTagIdList(externalUserInfoListDTO.getTagIds());
        customerSearchDTO.setCorpWxLastAddTimeStart(externalUserInfoListDTO.getAddStartTime());
        customerSearchDTO.setCorpWxLastAddTimeEnd(externalUserInfoListDTO.getAddEndTime());
        customerSearchDTO.setCorpWxFirstAddTimeStart(externalUserInfoListDTO.getFirstAddStartTime());
        customerSearchDTO.setCorpWxFirstAddTimeEnd(externalUserInfoListDTO.getFirstAddEndTime());
        //流程阶段筛选
        String processId = externalUserInfoListDTO.getProcessId();
        if(StringUtils.isNotBlank(processId)){
            customerSearchDTO.setCustomerProcessIdList(Collections.singletonList(processId));
        }
        String stageId = externalUserInfoListDTO.getStageId();
        if(StringUtils.isNotBlank(stageId)){
            customerSearchDTO.setCustomerStageIdList(Collections.singletonList(stageId));
        }
        //客户星级
        Integer customerRating = externalUserInfoListDTO.getCustomerRating();
        if(Objects.nonNull(customerRating)){
            customerSearchDTO.setCustomerRatingList(Collections.singletonList(customerRating));
        }
        if (StringUtils.isNotBlank(externalUserInfoListDTO.getPhone())) {
            customerSearchDTO.setPhone(externalUserInfoListDTO.getPhone());
        }
        customerSearchDTO.setAuthorizationState(externalUserInfoListDTO.getStatus());
        Date addFriendStartTime = externalUserInfoListDTO.getAddFriendStartTime();
        Date addFriendEndTime = externalUserInfoListDTO.getAddFriendEndTime();
        if (CollectionUtils.isNotEmpty(externalUserInfoListDTO.getOrgUserIdList())
                || externalUserInfoListDTO.getAddWay() != null || (Objects.nonNull(addFriendStartTime) && Objects.nonNull(addFriendEndTime))) {
            CorpWxUserInfoRequest corpWxUserInfoRequest = new CorpWxUserInfoRequest();
            corpWxUserInfoRequest.setOrgUserIdList(externalUserInfoListDTO.getOrgUserIdList());
            corpWxUserInfoRequest.setAddWay(externalUserInfoListDTO.getAddWay());
            corpWxUserInfoRequest.setAddTimeStart(addFriendStartTime);
            corpWxUserInfoRequest.setAddTimeEnd(addFriendEndTime);
            Integer subAddWay = externalUserInfoListDTO.getSubAddWay();
            if(Objects.nonNull(subAddWay)){
                corpWxUserInfoRequest.setSubAddWay(Collections.singletonList(subAddWay));
            }
            customerSearchDTO.setCorpWxUserInfoRequest(corpWxUserInfoRequest);
        }
        if(null != externalUserInfoListDTO.getAddMultiUsers()){
            //判断客户是否添加过多位员工
            if(externalUserInfoListDTO.getAddMultiUsers()){
                //大于等于2时表示添加过多位员工(客户搜索要求传 范围筛选) 10000基本业务不可能发生
                customerSearchDTO.setCorpWxUserCountBegin(2);
                customerSearchDTO.setCorpWxUserCountEnd(10000);
            }else{
                //小于等于1时只添加过一位员工(客户搜索要求传 范围筛选)
                customerSearchDTO.setCorpWxUserCountBegin(1);
                customerSearchDTO.setCorpWxUserCountEnd(1);

            }
        }
        if(null != externalUserInfoListDTO.getFollowStatus()){
            //判断是否已流失(已流失：和所有员工关系都是非好友） （未流失：至少和一位员工保持好友关系）
            if(externalUserInfoListDTO.getFollowStatus()){
                //小于等于0表示已流失(客户搜索要求传 范围筛选)
                customerSearchDTO.setCorpWxFriendCountBegin(0);
                customerSearchDTO.setCorpWxFriendCountEnd(0);
            }else{
                //大于等于1时表示未流失(客户搜索要求传 范围筛选)
                customerSearchDTO.setCorpWxFriendCountBegin(1);
                customerSearchDTO.setCorpWxFriendCountEnd(10000);
            }
        }
        customerSearchDTO.setPageSize(externalUserInfoListDTO.getPageSize());
        customerSearchDTO.setPageNum(externalUserInfoListDTO.getPageNum());
        customerSearchDTO.setSortField(SearchSortFieldEnum.CORP_WX_LAST_ADD_TIME);
        customerSearchDTO.setSortType(SearchSortTypeEnum.DESC);
        return customerSearchDTO;
    }

}
